﻿using RStudio.UIFramwork.Editor.Utils;
using RStudio.UIFramwork.Utils;
using UnityEditor;


namespace RStudio.UIFramwork.Editor
{
    [CanEditMultipleObjects]
    [CustomEditor(typeof(UIBase), true)]
    public class EditUIBase : UnityEditor.Editor
    {
        private UIBase m_UIBase;

        public void OnEnable()
        {
            m_UIBase = target as UIBase;
        }

        public override void OnInspectorGUI()
        {
            Version01();
        }

        public void Version01()
        {
            bool isGet = UIAssetUtil.IsUIDetailAttached(m_UIBase.GetType(), out UIDetailAttribute output);
            string description = isGet ? output.Description : "缺失";
            string assetLink = isGet ? output.AssetLink : "缺失";
            EditorGUILayout.LabelField($"页面描述：{description}", EditorStyles.boldLabel);
            EditorGUILayout.LabelField($"资产路径：{assetLink}", EditorStyles.boldLabel);


            EditorGUILayout.Space();

            EditorGUILayout.LabelField("功能区", EditorStyles.boldLabel);
            {

                EditorGUILayout.BeginHorizontal("box");
                {
                    EditUIComponentUtil.UnitButton("唯一实例", ref m_UIBase.UseAsSingle, m_UIBase);
                }
                EditorGUILayout.EndHorizontal();

                EditorGUILayout.BeginHorizontal("box");
                {
                    EditUIComponentUtil.UnitButton("区域关闭", ref m_UIBase.UseAreaQuit, m_UIBase);
                    EditUIComponentUtil.UnitButton("区域阻挡", ref m_UIBase.UseBlock, m_UIBase);
                    EditUIComponentUtil.UnitButton("区域遮罩", ref m_UIBase.UseMask, m_UIBase);
                }
                EditorGUILayout.EndHorizontal();
            }


            EditorGUILayout.Space();

            EditorGUILayout.LabelField("关旧配置", EditorStyles.boldLabel);

            EditorGUILayout.BeginHorizontal("box");
            {
                EditUIComponentUtil.UnitButton("关闭前页", ref m_UIBase.UseClosePrevious, m_UIBase);
                EditUIComponentUtil.UnitButton("允许关闭", ref m_UIBase.CanBeClosed, m_UIBase);
                EditUIComponentUtil.UnitButton("启用回退", ref m_UIBase.CanBeRecord, m_UIBase);
            }
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.Space();

            EditorGUILayout.LabelField("页面样式", EditorStyles.boldLabel);

            EditorGUILayout.BeginHorizontal("box");
            {
                EditUIComponentUtil.UnitButton("四周型", ref m_UIBase.UseQuardRato, m_UIBase);
                EditUIComponentUtil.UnitButton("居中型", ref m_UIBase.UseCentral, m_UIBase);
            }
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.Space();

            EditorGUILayout.LabelField("页面交互", EditorStyles.boldLabel);

            EditorGUILayout.BeginHorizontal("box");
            {
                EditUIComponentUtil.UnitButton("允许拖拽", ref m_UIBase.CanDrag, m_UIBase);
                EditUIComponentUtil.UnitInputField("", ref m_UIBase.UIDragSetting.Margin, m_UIBase);
                EditUIComponentUtil.UnitInputField("", ref m_UIBase.UIDragSetting.Space, m_UIBase);
            }
            EditorGUILayout.EndHorizontal();

            base.OnInspectorGUI();
        }




        #region 
        //private SerializedProperty attributesProp;
        //private bool[] foldouts;
        //public void Version02()
        //{
        //    serializedObject.Update();

        //    EditorGUILayout.LabelField("UI Attributes", EditorStyles.boldLabel);

        //    // 显示添加菜单按钮
        //    DisplayAddMenu();

        //    // 显示所有属性
        //    DisplayAttributes();

        //    serializedObject.ApplyModifiedProperties();

        //}      
        //private void DisplayAttributes()
        //{
        //    for (int i = 0; i < m_UIBase.AttributeLib.Count; i++)
        //    {
        //        IUIAttributeBehaviour attribute = m_UIBase.AttributeLib[i];

        //        EditorGUILayout.BeginVertical(EditorStyles.helpBox);
        //        {
        //            #region 标题

        //            EditorGUILayout.BeginHorizontal();
        //            {
        //                // 折叠箭头
        //                foldouts[i] = EditorGUILayout.Foldout(foldouts[i], GetTypeName(attribute.GetType()), true);

        //                // 启用/禁用开关
        //                attribute.IsEnabled = EditorGUILayout.ToggleLeft("启用", attribute.IsEnabled, GUILayout.Width(80));

        //                // 删除按钮
        //                if (GUILayout.Button("×", EditorStyles.miniButton, GUILayout.Width(20)))
        //                {
        //                    var uiBase = target as UIBase;

        //                    //uiBase.RemoveBeahviour(attribute);
        //                    EditorUtility.SetDirty(target);
        //                    return;
        //                }
        //            }
        //            EditorGUILayout.EndHorizontal();
        //            #endregion

        //            #region 展开内容
        //            //if (foldouts[i])
        //            //{
        //            //    EditorGUI.indentLevel++;

        //            //    // 显示序列化字段
        //            //    var iterator = element.Copy();
        //            //    var end = iterator.GetEndProperty();

        //            //    bool enterChildren = true;
        //            //    while (iterator.NextVisible(enterChildren))
        //            //    {
        //            //        if (SerializedProperty.EqualContents(iterator, end))
        //            //            break;

        //            //        EditorGUILayout.PropertyField(iterator, true);
        //            //        enterChildren = false;
        //            //    }

        //            //    EditorGUI.indentLevel--;
        //            //}
        //            #endregion
        //        }
        //        EditorGUILayout.EndVertical();
        //        EditorGUILayout.Space(5);
        //    }
        //}
        //private void DisplayAddMenu()
        //{
        //    EditorGUILayout.BeginHorizontal();
        //    EditorGUILayout.LabelField("Add Attribute:");

        //    if (EditorGUILayout.DropdownButton(new GUIContent("+"), FocusType.Passive, EditorStyles.miniButton))
        //    {
        //        GenericMenu menu = new();

        //        menu.AddItem(new GUIContent("Mask"), false, () => AddAttribute<UIFucUseMask>());
        //        menu.AddItem(new GUIContent("Block"), false, () => AddAttribute<UIFucUseBlock>());
        //        menu.AddItem(new GUIContent("ClosePrevious"), false, () => AddAttribute<UIFucClosePrevious>());

        //        menu.ShowAsContext();
        //    }

        //    EditorGUILayout.EndHorizontal();
        //}
        //private void AddAttribute<T>() where T : IUIAttributeBehaviour, new()
        //{
        //    var uiBase = target as UIBase;

        //    // 更新foldouts数组大小
        //    uiBase.AddBehaviour<T>();
        //    Array.Resize(ref foldouts, uiBase.AttributeLib.Count);
        //    foldouts[uiBase.AttributeLib.Count - 1] = true;

        //    EditorUtility.SetDirty(target);
        //}
        //private string GetTypeName(Type type)
        //{
        //    if (type == typeof(UIFucUseMask)) return "Mask";
        //    if (type == typeof(UIFucUseBlock)) return "Block";
        //    if (type == typeof(UIFucClosePrevious)) return "Close Previous";
        //    return type.Name;
        //}
        #endregion
    }
}
